package array;

/**
 * leet-code(240) 搜索二维矩阵
 *
 * @author Ringo
 * @date 2021/8/19 21:13
 */
public class SearchMatrix {

    public static void main(String[] args) {
        int[][] matrix = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
        System.out.println(searchMatrix(matrix, 5));
    }

    public static boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return false;
        int m = matrix.length - 1, n = 0;

        int v = matrix[m][n];

        while (v != target) {
            if (target < v)
                --m;
            else if (target > v)
                ++n;
            if (m < 0 || n >= matrix[0].length)
                return false;
            v = matrix[m][n];
        }
        return true;
    }

}
